package com.wu.client.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.wu.client.service.RankService;
import com.wu.client.vo.res.rank.RankDetailResp;
import com.wu.client.vo.res.rank.RankResp;
import com.wu.mongo.Rank;
import com.wu.mongo.RankDetail;
import lombok.RequiredArgsConstructor;
import org.bson.types.ObjectId;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Cousin
 * @date 2025/4/1
 * @Description
 */
@Service
@RequiredArgsConstructor
public class RankServiceImpl implements RankService {
    private final MongoTemplate mongoTemplate;

    @Override
    public List<RankResp> getRanks() {
        List<Rank> rankList = mongoTemplate.findAll(Rank.class);
        List<RankResp> rankRespList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(rankList)) {
            rankRespList = BeanUtil.copyToList(rankList, RankResp.class);
        }
        return rankRespList;
    }

    @Override
    public List<RankDetailResp> getRankDetail(String rankId) {
        Query query =new Query();
        query.addCriteria(Criteria.where("rankId").is(new ObjectId(rankId)));
        List<RankDetail> rankDetailList = mongoTemplate.find(query, RankDetail.class);
        if (CollectionUtil.isNotEmpty(rankDetailList)) {
            return BeanUtil.copyToList(rankDetailList, RankDetailResp.class);
        }
        return List.of();
    }
}
